package com.tuocent.dagv2.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.entity.UserModule;
import com.tuocent.dagv2.common.utils.PublicUtil;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.Modules;
import com.tuocent.dagv2.mapper.ModulesMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {
    @Resource
    private RedisCache redis;

    @Autowired
    private ModulesMapper modulesMapper;


    @RequestMapping("")
    public String Index(Model model, HttpServletResponse response, HttpSession session) {
        LoginUserInfo userInfo = redis.get(session.getId());
        if (userInfo.getRoles() != null && !userInfo.getRoles().isEmpty()) {
            //用户菜单
            List<Modules> pModulel = modulesMapper.userModule(new QueryWrapper<Modules>()
                    .in("a.roleid", userInfo.getRoles())
                    .eq("a.isuer", "1")
                    .groupBy("b.id")
                    .orderByAsc("model").orderByAsc("sort"));
            List<UserModule> userModules = new ArrayList<>();
            for (Modules modulel : pModulel) {
                if (modulel != null) {
                    if ("0".equals(modulel.getModel())) {
                        UserModule uModel = new UserModule();
                        uModel.setModulename(modulel.getModulename());
                        uModel.setTitle(modulel.getTitle());
                        uModel.setRoute(modulel.getRoute());
                        uModel.setImgidx(modulel.getImgidx());
                        uModel.setIsnav(modulel.getIsnav());
                        uModel.setPid(modulel.getPid());
                        uModel.setIsChildren("0");
                        for (Modules Children : pModulel) {
                            if (Children != null) {
                                if (modulel.getId().equals(Children.getPid())) {
                                    uModel.addChildren(Children);
                                    uModel.setIsChildren("1");
                                }
                            }
                        }
                        userModules.add(uModel);
                    }
                }
            }
            model.addAttribute("userMenu", userModules);
        }

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-store");
        model.addAttribute("userName", userInfo.getUserName());
        model.addAttribute("consolePage", "console");
        model.addAttribute("appName", PublicConst.APP_NAME);
        return "/home/index";
    }

    @RequestMapping("/home/console")
    public String console(Model model, HttpSession session) {
        LoginUserInfo userInfo = redis.get(session.getId());
        if (userInfo.getRoles() != null && !userInfo.getRoles().isEmpty()) {
            //用户菜单
            List<Modules> pModulel = modulesMapper.useLnkSortModule(new QueryWrapper<Modules>()
                    .eqSql("a.moduleid", "b.id")
                    .in("a.roleid", userInfo.getRoles())
                    .eq("a.isuer", "1")
                    .eq("b.lnk", "1")
                    .groupBy("b.id")
                    .orderByAsc("lnk_sort"));

            model.addAttribute("shortcuts", pModulel);
        }
        model.addAttribute("welcomeInfo", welcomeInfo());
        return "/home/console";
    }

    private String welcomeInfo() {
        LocalDate today = LocalDate.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        return "今天是：" + today.format(dtf) + " 星期" + PublicUtil.CN_WEEK[today.getDayOfWeek().getValue() - 1] + "；欢迎使用" + PublicConst.APP_NAME;
    }
}
